import requests
from lxml import etree
import time
import random

from .header_random import header_list
headers={
    "User-Agent":random.choice(header_list)
}

#搜索函数
def start_url(search):
    urls = "https://www.qb5200.tw/s.php?q=" + search
    response = requests.get(url=urls,headers=headers)
    html = etree.HTML(response.text)
    response.encoding = response.apparent_encoding
    all_fic = html.xpath("//div[@class='bookbox']/div[@class='p10']")
    for i in all_fic:
        fiction_title = i.xpath(".//h4[@class='bookname']/a/text()")[0]
        fiction_href = "https://www.qb5200.tw"+i.xpath(".//h4[@class='bookname']/a/@href")[0]
        images = "https://www.qb5200.tw"+i.xpath(".//div[@class='bookimg']/a/img/@src")[0]
        author = i.xpath(".//div[@class='bookinfo']/div[@class='author']/text()")[0]
        jianjie = i.xpath(".//div[@class='bookinfo']/p/text()")[0]
        timeArray = time.localtime(time.time())
        create_time =  time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
        yield fiction_title,fiction_href,jianjie,author,images,create_time,1

        # zhangjie(fiction_href,fiction_id)
#章节爬取
def zhangjie(fiction_href,fiction_id):
    print(fiction_href)
    response = requests.get(url=fiction_href,headers=headers)
    html = etree.HTML(response.text)
    all_zhang = html.xpath("//div[@class='listmain']/dl/dd/a")[9:20]
    for i in all_zhang:
        title = i.xpath("./text()")[0]
        hrefs= 'https://www.qb5200.tw'+i.xpath("./@href")[0]

        yield  title, hrefs, fiction_id




def content(zhangjie_id,zhang_href):
    response =requests.get(url=zhang_href,headers = headers)
    time.sleep(random.randint(1, 2))
    html = etree.HTML(response.text)
    context =''.join( html.xpath("//div[@id='content']/text()"))
    title = html.xpath("//div[@class='content']/h1/text()")[0]
    yield zhangjie_id,title,context








